package top.juanshen.print.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.util.ThreadUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juanshen.print.escppos.PrintJob;
import top.juanshen.print.escppos.PrintQueue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;


/**
 * @author yanjuan
 * @date 17/11/9 下午12:40
 */

@RestController
public class PrintController {

    public static ExecutorService executorService = ThreadUtil.newExecutor();

    @RequestMapping("index")
    public String index(){
        mySleep(2);
        System.out.println("add print job...");
        PrintJob printJob = new PrintJob(0, jsonParam(), "jimmymiao.nat123.net");
        //PrintJob printJob = new PrintJob(0, jsonParam(), "116.228.222.198");
        executorService.submit(printJob);
        PrintQueue.add(printJob);
        mySleep(6);
        return "打印一只军师成功";
    }

    public void mySleep(int i) {
        try {
            System.out.println("sleep" + i*1000 + "ms ...");
            Thread.sleep(i * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public  Map<String, Object> jsonParam(){
        Map<String, Object> template = new HashMap<String, Object>();
        Map<String, Object> keys = new HashMap<String, Object>();
        String posResult = "重单 请不要重复下单，流水号:社会我头哥--";
        keys.put("title", "网络订单");
        keys.put("brandName", "智慧餐厅");
        keys.put("shopName", "天山店");
        keys.put("tableNumb", "0102");
        keys.put("tableName", "魔都厅");
        keys.put("orderId", "1609101220001");
        keys.put("dateTime", "2016-09-10 12:21:00");
        keys.put("allPrice", "499.0");
        keys.put("barCode","8888");

        if(!posResult.contains("成功")){
            if(posResult.contains("重单")){
                keys.put("warnTitle", "提    示");
                keys.put("warnMsg", "该订单已处理");
            }else{
                keys.put("warnTitle", "异常提示");
                keys.put("warnMsg", "自动下单失败请人工处理");
            }
        }else{
            if(posResult.contains("无此") || posResult.contains("沽清")
                    || posResult.contains("不存在")){
                keys.put("warnTitle", "异常提示");
                keys.put("warnMsg", "部分菜品未下成功人工处理");
            }
        }

        keys.put("posTitle", "收银软件下单结果");
        keys.put("posMsg", posResult);


        List<Map<String, Object>> goods = new ArrayList<Map<String,Object>>();

        template.put("goods", goods);
        template.put("keys", keys);
        System.out.println(JSONObject.toJSONString(template));
        return template;
    }


}
